package top.zsyle.system.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import top.zsyle.system.domain.Menu;
import top.zsyle.system.domain.Role;
import top.zsyle.system.mapper.MenuMapper;
import top.zsyle.system.mapper.PermissionMapper;
import top.zsyle.system.mapper.RoleMapper;
import top.zsyle.system.service.IRoleService;
import top.zsyle.basic.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zsy
 * @since 2022-11-29
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private PermissionMapper permissionMapper;

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public void add(Role role) {
        super.add(role);
        //添加 t_role_permisstion中间表
        permissionMapper.addMiddle(role.getId(),role.getPermissions());
        //添加 t_role_menu中间表
        menuMapper.addMiddle(role.getId(),role.getMenus());
    }

    @Override
    public void update(Role role) {
        super.update(role);
        //修改 t_role_permisstion中间表   **先删除再添加
        permissionMapper.deleteMiddleByRoleId(role.getId());
        permissionMapper.addMiddle(role.getId(),role.getPermissions());
        //修改 t_role_menu中间表  **先删除再添加
        menuMapper.deleteMiddleByRoleId(role.getId());
        menuMapper.addMiddle(role.getId(),role.getMenus());
    }

    @Override
    public void delete(Long id) {
        permissionMapper.deleteMiddleByRoleId(id);
        //删除中间表 t_role_permisstion
        menuMapper.deleteMiddleByRoleId(id);
        //删除 t_role_menu中间表 中的数据
        super.delete(id);
    }
}
